<template>
  <div class='container'>
    <el-dialog :title="showTitle" :visible="showDialog" :close-on-click-modal="false" @close="closeDialog" width="500px">
      <el-form ref="form" :model="form"    :rules="rules" label-width="80px">
        <el-form-item v-if="this.nodeData.subjectID==undefined" label="所属学科">
          <el-select  placeholder="请选择" v-model="form.subjectID">
            <!--  -->
            <el-option v-for="item in list" :key="item.id" :label="item.subjectName" :value="item.id" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName" >
          <el-input v-model="form.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary"  @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add as addAPI, update as updateAPI } from '@/api/hmmm/directorys'
import { list as subjectAPI } from '@/api/hmmm/subjects'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      default: () => {}
    },
    form: {
      type: Object,
      default: () => {}
    }
    // subjectId: {
    //   type: Object,
    //   default: () => {}
    // }
  },
  created () {
    this.subjectList()
  },
  computed: {
    // 弹框对应的标题
    showTitle () {
      if (!this.nodeData.id) {
        return '新增目录'
      } else {
        return '修改目录'
      }
    }
  },
  data () {
    return {

      rules: {
        directoryName: [
          { required: true, message: '目录名称不能为空哦', trigger: 'blur' }
        ]
      },
      list: [],
      Data: {
        page: 1,
        pagesize: 9999,
        subjectName: ''
      }
    }
  },
  methods: {
    /**
     * 关闭弹窗
     */
    closeDialog () {
      this.$emit('update:showDialog', false)
      this.form.subjectID = ''
      this.$refs.form.resetFields()
    },
    /**
     * 获取弹窗下拉框数据
     */
    async subjectList () {
      const { data } = await subjectAPI(this.Data)
      // console.log(data)
      this.list = data.items
    },

    /**
       * 点击确定
       */
    submitForm () {
      this.$refs.form.validate(async flag => {
        if (!flag) return
        if (this.nodeData.id) {
          if (this.nodeData.subjectID === undefined) {
            await updateAPI({ id: this.nodeData.id, subjectID: this.form.subjectID, directoryName: this.form.directoryName })
            this.$message.success('修改成功')
          } else {
            await updateAPI({ id: this.nodeData.id, subjectID: this.nodeData.subjectID, directoryName: this.form.directoryName })
            this.$message.success('修改成功')
          }
        } else {
          if (this.nodeData.subjectID === undefined) {
            await addAPI(this.form)
          } else {
            await addAPI({ subjectID: this.nodeData.subjectID, directoryName: this.form.directoryName })
          }
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.$parent.dataList()
      })
    }

  }

}

</script>

<style scoped lang='less'></style>
